﻿namespace hedefgrup.reports.renault.date
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;

    public class reportMonth
    {
        private static DateTimeFormatInfo dateTimeFormats = DateTimeFormatInfo.GetInstance(CultureInfo.GetCultureInfo("tr-TR"));
        public int dateValue;

        public reportMonth(DateTime date)
            : this(Convert.ToDouble((int)((date.Year * 100) + date.Month)))
        {
        }

        public reportMonth(string yyyyMM)
            : this(Convert.ToInt32(yyyyMM.Substring(0, 4)), Convert.ToInt32(yyyyMM.Substring(4, 2)))
        {

        }

        private reportMonth(double dateValue)
        {
            if (((dateValue < 100.0) || ((dateValue % 100.0) > 12.0)) || ((dateValue % 100.0) < 1.0))
            {
                throw new ArgumentException("invalid value");
            }
            this.dateValue = Convert.ToInt32(dateValue);
        }

        public reportMonth(int dateValue)
            : this(Convert.ToDouble(dateValue))
        {
        }

        public reportMonth(int year, int month)
            : this(Convert.ToDouble((int)((year * 100) + month)))
        {
        }

        public DateTime getAsDateTime()
        {
            return new DateTime(this.getYear(), this.getMonth(), 1);
        }

        public int getMonth()
        {
            return Convert.ToInt32((int)(this.dateValue % 100));
        }

        public string getMonthName()
        {
            return dateTimeFormats.MonthNames[this.getMonth() - 1];
        }

        public static reportMonth[] getMonthsInInterval(reportMonth startMonth, reportMonth endMonth)
        {
            int num5;
            if (startMonth.getYear() > endMonth.getYear())
            {
                reportMonth month = startMonth;
                startMonth = endMonth;
                endMonth = month;
            }
            int year = startMonth.getYear();
            int num2 = endMonth.getYear();
            int num3 = startMonth.getMonth();
            int num4 = endMonth.getMonth();
            List<reportMonth> list = new List<reportMonth>();
            if (year == num2)
            {
                for (num5 = Math.Min(num3, num4); num5 <= Math.Max(num3, num4); num5++)
                {
                    list.Add(new reportMonth(year, num5));
                }
            }
            else
            {
                for (num5 = year; num5 <= num2; num5++)
                {
                    list.AddRange((num5 == year) ? ((IEnumerable<reportMonth>)getMonthsInInterval(startMonth, new reportMonth(startMonth.getYear(), 12))) : ((num5 == num2) ? ((IEnumerable<reportMonth>)getMonthsInInterval(new reportMonth(endMonth.getYear(), 1), endMonth)) : ((IEnumerable<reportMonth>)getMonthsInInterval(new reportMonth(num5, 1), new reportMonth(num5, 12)))));
                }
            }
            return list.ToArray();
        }

        public int getMonthYearValue()
        {
            return ((this.getYear() * 12) + this.getMonth());
        }

        public int getYear()
        {
            return Convert.ToInt32((int)((this.dateValue - (this.dateValue % 100)) / 100));
        }

        public static reportMonth operator +(reportMonth m, int months)
        {
            if (months < 0)
            {
                return (m - Math.Abs(months));
            }
            reportMonth month = new reportMonth(m.getYear(), m.getMonth());
            for (int i = 0; i < months; i++)
            {
                month++;
            }
            return month;
        }

        public static reportMonth operator --(reportMonth m)
        {
            m.dateValue -= (m.getMonth() == 1) ? 0x59 : 1;
            return m;
        }

        public static reportMonth operator ++(reportMonth m)
        {
            m.dateValue += (m.getMonth() == 12) ? 0x59 : 1;
            return m;
        }

        public static reportMonth operator -(reportMonth m, int months)
        {
            if (months < 0)
            {
                return (m + Math.Abs(months));
            }
            reportMonth month = new reportMonth(m.getYear(), m.getMonth());
            for (int i = 0; i < months; i++)
            {
                month--;
            }
            return month;
        }

        public string ToYYYYMM()
        {
            return string.Format("{0:0000}{1:00}", this.getYear(), this.getMonth());
        }

        public override string ToString()
        {
            return string.Format("{0:0000} - {1:00}", this.getYear(), this.getMonth());
        }
    }
}

